<template>
  <div>
    <van-field v-model="content" />
    <van-button @click="send">发送</van-button>
  </div>
</template>

<script>
import { TenonWebsocket } from "@/js/websocket";
import { Button, Field } from "vant";
const CMD = "test";
const SUBCMD = "websocket";
export default {
  components: {
    [Button.name]: Button,
    [Field.name]: Field,
  },
  data() {
    return {
      content: 0,
      ready: false,
    };
  },
  computed: {
    appData() {
      return this.$store.state.appData;
    },
  },
  watch: {
    appData: {
      deep: true,
      immediate: true,
      handler(val, oldVal) {
        console.log("appDataChange", val, oldVal);
        if (val && val.resQuery && val.jsApiTicket) {
          //   this.initData(val.resQuery, val.jsApiTicket);
        }
      },
    },
  },
  mounted() {
    this.initData();
  },
  methods: {
    initData(resQuery, jsApiTicket) {
      let userId = this.$route.query.id || 0;
      let aid = 1;
      TenonWebsocket.msgHandlerCallbackMap.set(CMD + SUBCMD, (data) => {
        console.log("res", data);
      });
      TenonWebsocket.init(`ws://192.168.110.43:8220/tmr/websocket.ws/${userId}`)
        .then(() => {
          this.ready = true;
        })
        .catch((err) => {
          this.ready = false;
          console.log(err);
        });
    },
    send() {
      if (this.ready) {
        TenonWebsocket.sendData(
          JSON.stringify({
            Cmd: CMD,
            SubCmd: SUBCMD,
            msg: this.content,
          })
        );
      }
    },
  },
};
</script>

<style>
</style>